package com.zxy.javaarithmetic;

import com.zxy.javaarithmetic.bean.ListNode;
import com.zxy.javaarithmetic.bean.TreeNode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author : Shonn.Zhang
 * @email : shonn.zhang@mart.blue
 * @time : 2021/8/27 11:42
 * @desc :
 */
public class Utils {

    public static void print(int[] arr, int left, int right) {
        for (int i = left; i <= right; i++) {
            System.out.print(" " + arr[i]);
        }
        System.out.println();
    }

    public static void print(int[] arr) {
        print(arr, 0, arr.length - 1);
    }

    public static void printListNode(ListNode node) {
        while (node != null) {
            System.out.print(" " + node.val);
            node = node.next;
        }
        System.out.println();
    }

    /**
     * 将数组转换成二叉树
     * @param arr 值为null时表示无节点
     * @return
     */
    public static TreeNode createBinaryTree(Integer[] arr){
        Queue<TreeNode> queue = new ArrayDeque<>();
        TreeNode root = new TreeNode(arr[0]);
        queue.add(root);
        int index = 1;
        while (!queue.isEmpty() && index < arr.length){
            TreeNode treeNode = queue.poll();
            if(arr[index] != null){
                TreeNode left = new TreeNode(arr[index]);
                treeNode.left = left;
                queue.add(left);
            }
            index++;
            if(index < arr.length && arr[index] != null){
                TreeNode right = new TreeNode(arr[index]);
                treeNode.right = right;
                queue.add(right);
            }
            index++;
        }
        return root;
    }
}
